/**
 * IndexedDB数据库管理器
 * 整合所有存储模块，提供统一的接口
 */
import { DBManager } from './dbBase';
import { ChatStorage, ChatRecord, ChatMessage, initChatStorage } from './chatStorage';
import { LigHistoryStorage, LigHistoryRecord, initLigHistoryStorage } from './ligHistoryStorage';

/**
 * IndexedDB管理器实现类
 */
class AppIndexedDBManager extends DBManager {
    private chatStorage: ChatStorage;
    private ligHistoryStorage: LigHistoryStorage;
    private storeInitializers: ((db: IDBDatabase) => void)[] = [];

    constructor() {
        super();

        // 注册存储初始化器
        this.registerStoreInitializer(initChatStorage);
        this.registerStoreInitializer(initLigHistoryStorage);

        // 初始化存储模块
        this.chatStorage = new ChatStorage(this);
        this.ligHistoryStorage = new LigHistoryStorage(this);
    }

    /**
     * 注册存储初始化器
     */
    private registerStoreInitializer(initializer: (db: IDBDatabase) => void): void {
        this.storeInitializers.push(initializer);
    }

    /**
     * 创建所有存储对象
     */
    protected createObjectStores(db: IDBDatabase): void {
        // 调用所有注册的初始化器
        for (const initializer of this.storeInitializers) {
            initializer(db);
        }
    }

    // ==================== 聊天记录操作 ====================

    /**
     * 添加聊天记录
     */
    async addChat(chat: ChatRecord): Promise<boolean> {
        return this.chatStorage.add(chat);
    }

    /**
     * 更新聊天记录
     */
    async updateChat(chat: ChatRecord): Promise<boolean> {
        return this.chatStorage.update(chat);
    }

    /**
     * 删除聊天记录
     */
    async deleteChat(chatId: string): Promise<boolean> {
        return this.chatStorage.delete(chatId);
    }

    /**
     * 获取单条聊天记录
     */
    async getChat(chatId: string): Promise<ChatRecord | null> {
        return this.chatStorage.get(chatId);
    }

    /**
     * 获取所有聊天记录
     */
    async getAllChats(): Promise<ChatRecord[]> {
        return this.chatStorage.getAll();
    }

    /**
     * 向聊天添加新消息
     */
    async addMessageToChat(chatId: string, message: ChatMessage): Promise<boolean> {
        return this.chatStorage.addMessage(chatId, message);
    }

    // ==================== LigHistory记录操作 ====================

    /**
     * 添加LigHistory记录
     */
    async addLigHistory(record: LigHistoryRecord): Promise<boolean> {
        return this.ligHistoryStorage.add(record);
    }

    /**
     * 批量添加LigHistory记录
     */
    async addLigHistoryBatch(records: LigHistoryRecord[]): Promise<boolean> {
        return this.ligHistoryStorage.addBatch(records);
    }

    /**
     * 更新LigHistory记录
     */
    async updateLigHistory(record: LigHistoryRecord): Promise<boolean> {
        return this.ligHistoryStorage.update(record);
    }

    /**
     * 删除LigHistory记录
     */
    async deleteLigHistory(id: number): Promise<boolean> {
        return this.ligHistoryStorage.delete(id);
    }

    /**
     * 获取单条LigHistory记录
     */
    async getLigHistory(id: number): Promise<LigHistoryRecord | null> {
        return this.ligHistoryStorage.get(id);
    }

    /**
     * 获取所有LigHistory记录
     */
    async getAllLigHistory(): Promise<LigHistoryRecord[]> {
        return this.ligHistoryStorage.getAll();
    }

    /**
     * 清空所有LigHistory记录
     */
    async clearAllLigHistory(): Promise<boolean> {
        return this.ligHistoryStorage.clearAll();
    }
}

// 创建实例并导出
const dbManager = new AppIndexedDBManager();
export type IndexedDBManager = AppIndexedDBManager;

export {
    dbManager,
    AppIndexedDBManager,
    type ChatRecord,
    type ChatMessage,
    type LigHistoryRecord
}; 